import { Injectable } from "@nestjs/common";
import { AppGateway } from "../app/app.gateway";

@Injectable()
export class DicomService {
  constructor(private readonly appGateway: AppGateway) {
  }

  handleScan(scanParams){
    this.startBtnChange()
    const series = scanParams.scanSeries;
    series.forEach((eachSeries,index) => {
      const scanType = eachSeries.scanType;
      if (scanType === "scout") {
        const scoutType = eachSeries.scanParams.scoutType;
        const scoutCount = scoutType.includes("+") ? 2 : 1;
        console.log(`平片数量为:${scoutCount}`);
        setTimeout(() => {
          this.appGateway.sendDicomMsg({
            scanId: scanParams.scanSeries[0].scanId,
            imageIds: {
              studyUID: "1.2.2046.7929.09.1.20230724.105506.19135",
              seriesUID: "1.2.2046.7929.09.1.20230724.105506.07036"
            }
          });
        }, 5000);

        if (scoutCount === 2) {
          setTimeout(() => {
            this.appGateway.sendDicomMsg({
              scanId: scanParams.scanSeries[0].scanId,
              imageIds: {
                studyUID: "1.2.826.0.1.3680043.2.461.13044537.2800407881",
                seriesUID: "1.2.840.113619.2.472.3.2831161356.403.1654067940.232.6295553"
              }
            });
          }, 6000);
        }
      }
      else {
        if (index % 2 === 0) {
          setTimeout(() => {
            this.appGateway.sendDicomMsg({
              scanId: scanParams.scanSeries[index].scanId,
              imageIds: {
                studyUID: "1.2.2046.7929.09.1.20230724.105506.19135",
                seriesUID: "1.2.2046.7929.09.1.20230724.105506.07036"
              }
            });
          }, 5000 + index * 1000);
        }else {
          setTimeout(() => {
            this.appGateway.sendDicomMsg({
              scanId: scanParams.scanSeries[index].scanId,
              imageIds: {
                studyUID: "1.2.826.0.1.3680043.2.461.13044537.2800407881",
                seriesUID: "1.2.840.113619.2.472.3.2831161356.403.1654067940.232.6295553"
              }
            });
          }, 5000 + index * 1000);
        }

      }
    });
  }

  startBtnChange() {
    setTimeout(() => {
      this.appGateway.sendSysMsg({
        type: "scanBtnStatus",
        data: {
          status: "move"
        }
      });
    }, 1000);

    setTimeout(() => {
      this.appGateway.sendSysMsg({
        type: "scanBtnStatus",
        data: {
          status: "scan"
        }
      });
    }, 2000);

    setTimeout(() => {
      this.appGateway.sendSysMsg({
        type: "scanBtnStatus",
        data: {
          status: "scanClicked"
        }
      });
    }, 3000);

    setTimeout(() => {
      this.appGateway.sendSysMsg({
        type: "scanBtnStatus",
        data: {
          status: "inActive"
        }
      });
    }, 4000);
  }
}
